package g1.thermostat;

import android.app.Application;
import android.util.Log;

import java.util.HashMap;

public class Variables extends Application {
	
	private double nightTemperature = 18;
	public double getNightTemperature() {
        return nightTemperature;
    }
    public void setNightTemperature(double nT) {
        this.nightTemperature = nT;
    }
	
    private double dayTemperature = 21;
	public double getDayTemperature() {
        return dayTemperature;
    }
    public void setDayTemperature(double dT) {
        this.dayTemperature = dT;
    }
    
    private double ovTemperature = 7;
	public double getOvTemperature() {
        return ovTemperature;
    }
    public void setOvTemperature(double oT) {
        this.ovTemperature = oT;
    }
    
    //if true the override temperature overrides the schedule
    private boolean ovSchedule;
    public boolean getOvSchedule(){
    	return ovSchedule;
    }
    public void setOvSchedule(boolean ovs){
    	this.ovSchedule=ovs;
    }
    
    //if true the override temperature is enable
    private boolean ovEnable = false;
    public boolean getOvEnable(){
    	return ovEnable;
    }
    public void setOvEnable(boolean ove){
    	this.ovEnable=ove;
    }
    
    private boolean save4 = false;
    public boolean getSave4(){
    	return save4;
    }
    public void setSave4(boolean s4){
    	this.save4=s4;
    }
    
    private boolean save2 = false;
    public boolean getSave2(){
    	return save2;
    }
    public void setSave2(boolean s2){
    	this.save2=s2;
    }
    
//    private int currentTime;
//    public int getCurrentTime(){
//    	return currentTime;
//    }
//    public void setCurrentTime(int ct){
//    	this.currentTime=ct;
//    }


//-----------------------------------Weekly Schedule and Date---------------------------//

    final String[] WEEKDAYS = {"monday", "tuesday" , "wednesday","thursday", "friday", "saturday", "sunday"};

    public HashMap<String, DayNightTimes> timesList;

    boolean changedSchedule = false;

    public void createTimesList() {
	
    	timesList = new HashMap<String, DayNightTimes>();
	
    	for (int i=0;i<WEEKDAYS.length;i++) {
    		DayNightTimes times = new DayNightTimes();
    		timesList.put(WEEKDAYS[i],times);
    	}
    }

    public void addTimeInList(String r, String changeTo, int time) {
    	DayNightTimes timearrays = new DayNightTimes();
    	timearrays = timesList.get(r);
    
    	if (changeTo.equals("day")) {
    		timearrays.addTime(timearrays.day, time);
    	}
	
    	else if (changeTo.equals("night")) {
    		timearrays.addTime(timearrays.night, time);
    	}
    	timearrays.updateChanges();
    	timesList.put(r, timearrays);
    }

    public void delTimeInList(String r, String changeTo, int time) {
    	DayNightTimes timearrays = new DayNightTimes();
    	timearrays = timesList.get(r);
	
    	if (changeTo.equals("day")) {
    		timearrays.delTime(timearrays.day, time);
    	}
	
    	else if (changeTo.equals("night")) {
    		timearrays.delTime(timearrays.night, time);
    	}
	
    	timearrays.updateChanges();
    	timesList.put(r, timearrays);
    }


    public HashMap<String,DayNightTimes> getWeekSchedule() {
    	return timesList;
	}
    
    public void setWeekSchedule(HashMap<String, DayNightTimes> r) {
    	timesList = r;
    	changedSchedule = true;
	}
    
    
    
    public int getNextChange(int code) {
    	int dayOfWeek = code/100000;
    	int State = (code / 10000)%10;
    	Log.i("Phase0",""+State);
    	int timer = code % 10000;
    	int newCode = 9999;
    	
    	DayNightTimes dayarray = new DayNightTimes();
    	
    	
    	for(int i=0;i<5;i++) {
    		
    		if (i==0) {
    			Log.i("Phase1","Yes1");
    			if (State == 0) {
    				dayarray = timesList.get(WEEKDAYS[dayOfWeek]);
					newCode = dayOfWeek*100000 + 10000 + dayarray.getNextTime(dayarray.day, timer);
					if (newCode%10000 < 2400 && newCode%10000 > timer) {
						i=5;
					}
        		
				}
			
				else if (State == 1) {
					Log.i("Phase1A","Yes1A");
					dayarray = timesList.get(WEEKDAYS[dayOfWeek]);
					newCode = dayOfWeek*100000 + dayarray.getNextTime(dayarray.night, timer);
					if (newCode%10000 < 2400 && newCode%10000 > timer) {
						i=5;
					}
				}
    		}
    		
    		if (i==1) {
    			Log.i("Phase2","Yes2");
    			for (int j=(dayOfWeek+1); j<WEEKDAYS.length;j++) {
    				dayarray = timesList.get(WEEKDAYS[j]);
    				if (State == 0) {
    					newCode = j*100000 + 10000 + dayarray.getNextTime(dayarray.day, -1);
    					Log.i("NewCode1",""+newCode);
    					if (newCode%10000 < 2400) {
    						j=WEEKDAYS.length;
    						i=5;
    					}          		
    				}
    			
    				else if (State == 1) {
    					newCode = j*100000 + dayarray.getNextTime(dayarray.night, -1);
    					Log.i("NewCode1",""+newCode);
    					if (newCode%10000 < 2400) {
    						
    						j=WEEKDAYS.length;
    						i=5;
    					}
    				}
    			}
    		}
    		
    		if(i==2) {
    			Log.i("Phase3","Yes3");
    			for (int j=0; j<dayOfWeek;j++) {
    				dayarray = timesList.get(WEEKDAYS[j]);
        		
    				if (State == 0) {
    					newCode = j*100000 + 10000 + dayarray.getNextTime(dayarray.day, -1);
    					Log.i("NewCode2",""+newCode);
    					if (newCode%10000 < 2400) {
    						j = dayOfWeek;
    						i=5;
    					}
    				}
        		
    				else if (State == 1) {
    					newCode = j*100000 + dayarray.getNextTime(dayarray.night, -1);
    					Log.i("NewCode2",""+newCode);
    					if (newCode%10000 < 2400) {
    						j = dayOfWeek;
    						i=5;
    					}
    				}
    			}
    		}
    		
    		if (i==3) {
    			Log.i("Phase4","Yes4");
    			if (State == 0) {
    				dayarray = timesList.get(WEEKDAYS[dayOfWeek]);
					newCode = dayOfWeek*100000 + 10000 + dayarray.getNextTime(dayarray.day, timer);
					Log.i("NewCode3",""+newCode);
					if (newCode%10000 < 2400 && newCode%10000 < timer) {
						i=5;
					}
        		
				}
			
				else if (State == 1) {
					dayarray = timesList.get(WEEKDAYS[dayOfWeek]);
					newCode = dayOfWeek*100000 + dayarray.getNextTime(dayarray.night, timer);
					Log.i("NewCode3",""+newCode);
					if (newCode%10000 < 2400 && newCode%10000 < timer) {
						i=5;
					}
				}
    		}
    	
    		if (i==4) {
    			Log.i("Phase5","Yes5");
    			newCode = -1;
    			Log.i("NewCode4",""+newCode);
    			i=5;
    		}
    	}
    	return newCode;
    }
    
    boolean checkChangedSchedule() {
    	boolean tempbool = changedSchedule;
    	changedSchedule = false;
    	return tempbool;
    }
}
